<?php


namespace app\general\controller;


use app\general\model\SqArticleHot;
use phpspider\core\requests;
use phpspider\core\selector;
use think\Controller;

/**
 * 人人头条资讯爬取
 */
class PersonArticle extends Controller
{
    public static $article_ren_genre = [
        "news_hot"=>"热点",
        "news_tech"=>"科技",
        "news_entertainment"=>"娱乐",
        "news_game"=>"游戏",
        "news_sports"=>"体育",
        "news_finance"=>"财经",
        "news_military"=>"军事",
        "news_fashion"=>"时尚",
        "news_travel"=>"旅游",
        "news_discovery"=>"探索",
        "news_baby"=>"育儿",
        "news_regimen"=>"养生",
        "news_essay"=>"美文",
        "news_history"=>"历史",
        "news_food"=>"美食"
    ];
    public function clicb(){
        foreach (self::$article_ren_genre as $k=>$value){
            $url="https://www.toutiao.com/api/pc/feed/?category=$k&utm_source=toutiao";
            $data=$this->curl_get($url);
            $formData=array();
            foreach ($data['data'] as $art){
                $title=array("环球网","中国基金报","时代周报");
                if(!in_array($art['source'],$title)){
                    $url2="https://www.toutiao.com".$art['source_url'];
                    if(isset($art['middle_image'])){
                        $content=$this->getContent($url2);
                        if($content){
                            $content = preg_replace("/\\\\u([0-9a-f]{3,4})/i", "&#x\\1;", $content);
                            $content = html_entity_decode($content, null, 'UTF-8');
                            $content=htmlspecialchars_decode($content);
                            $content=str_replace('\"', '', $content);
                            $content=str_replace('"', '', $content);
                            if(is_array($art['middle_image'])){
                                $thumb=$art['middle_image']['url'];
                            }else{
                                $thumb=$art['middle_image'];
                            }
                            $title=str_replace('"', '', $art['title']);
                            $temp=array(
                                "title"=>$title,
                                "article_ren_genre"=>$value,
                                "thumb"=>$thumb,
                                "source"=>$art['source'],
                                "author"=>"",
                                "article_type"=>0,
                                "content"=> $content,
                                "create_time"=>time()
                            );
                            $formData[]=$temp;
                        }
                    }
                }
            }
            if(count($formData)>0){
                $id=$this->recordTimers(9);
                $flag=(new SqArticleHot())->saveAll($formData);
                $message= ($flag)?"今日头条".$value."栏目文章爬取成功":"今日头条".$value."暂无最新数据";
                $this->updateTimerType($id,$message);
            }
        }
        return "爬取成功";
    }
    /**
     * curl get
     */
    public function curl_get($url){
        // 防止CURL执行超时
        set_time_limit(0);
        // 请求地址
        // 初始化一个新会话
        $ch = curl_init();
        // 设置要求请的url
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        // 验证证书中域名是否与当前域名匹配 和上面参数配合使用
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $cookie="tt_webid=6769442829745391118; WEATHER_CITY=%E5%8C%97%E4%BA%AC; s_v_web_id=4272ea0d6b8a7012846ea585321e02b2; __tasessionId=r7vb6lvtl1576133946666; tt_webid=6769442829745391118; csrftoken=c9f366600097d9e546c63d4cbcf7a19e";
        curl_setopt($ch, CURLOPT_COOKIE, $cookie);
        //执行CURL请求
        $output = curl_exec($ch);
        // 关闭CURL资源
        curl_close($ch);
        // 输出返回信息
        return json_decode($output,true);
    }
    /**
     * 爬取文章内容
     */
    public function getContent($url){
        $html = requests::get($url);
        $selector="//div[contains(@class,'article-content')]";
        $content=selector::select($html, $selector,'xpath');
        if($content){
            return $content;
        }
        $selector="//div[contains(@class,'a-con')]";
        $content=selector::select($html, $selector,'xpath');
        if($content!=NULL){
            return $content;
        }
        $content=$this->strCutByStr($html,"content: '","'.slice");
        return $content;
    }

    /**
     * 截取字符串
     */
    function strCutByStr(&$str, $findStart, $findEnd = false, $encoding = 'utf-8'){
        if(is_array($findStart)){
            if(count($findStart) === count($findEnd)){
                foreach($findStart as $k => $v){
                    if(($result = strCutByStr($str, $v, $findEnd[$k], $encoding)) !== false){
                        return $result;
                    }
                }
                return false;
            }else{
                return false;
            }
        }

        if(($start = mb_strpos($str, $findStart, 0, $encoding)) === false){
            return false;
        }

        $start += mb_strlen($findStart, $encoding);

        if($findEnd === false){
            return mb_substr($str, $start, NULL, $encoding);
        }

        if(($length = mb_strpos($str, $findEnd, $start, $encoding)) === false){
            return false;
        }

        return mb_substr($str, $start, $length - $start, $encoding);
    }
    //记录定时器调用时间
    public function recordTimers($type)
    {
        $data = [
            'type' => $type,
            'create_time' => time(),
            'status' => 0
        ];
        return db('sq_record_timer')->insertGetId($data);
    }

    public function updateTimerType($id,$remark='')
    {
        db('sq_record_timer')->where('id', $id)->update(['status'=>1, 'remark'=>$remark]);
    }
}